package com.zhongyi;

import com.zhongyi.dao.IUserDao;
import com.zhongyi.domain.QueryVo;
import com.zhongyi.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.management.Query;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Mybatis_CRUD {

    private InputStream in;
    private SqlSession sqlSession;
    private IUserDao userDao;

    @Before
    public void init() throws IOException {
        //1.读取配置文件，生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.获取SqlSession对象
        sqlSession = factory.openSession();
        //4.获取dao的代理对象
        userDao = sqlSession.getMapper(IUserDao.class);
    }

    @After
    public void destroy() throws IOException {
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }

    @Test
    public void testFindAll(){
        //5.执行查询所有方法
        List<User> users = userDao.findAll();
        for(User user : users){
            System.out.println(user);
        }
    }

    @Test
    public void testFindById(){
        User user = userDao.findById(1);
        System.out.println(user);
    }

    @Test
    public void testAddUser(){
        User user = new User();
        user.setAddress("广州");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setUsername("王五");
        System.out.println("保存操作之前："+user);
        Integer i = userDao.saveUser(user);
        System.out.println("保存操作之后："+user);
    }

    @Test
    public void testUpdateUser(){
        User user = userDao.findById(6);
        user.setAddress("印度尼西峡");
        Integer i = userDao.updateUser(user);
        System.out.println(i);
    }

    @Test
    public void testDeleteUser(){
        int i = userDao.deleteUserById(5);
        System.out.println(i);
    }

    @Test
    public void testFinddByName(){
        List<User> users = userDao.findByName("张");
        for(User u:users){
            System.out.println(u);
        }
    }

    @Test
    public void testfindTotal(){
        int i = userDao.findTotal();
        System.out.println(i);
    }

    @Test
    public void testfindByVo(){
        QueryVo vo = new QueryVo();
        User user = new User();
        user.setUsername("%王%");
        vo.setUser(user);
        List<User> users = userDao.findByVo(vo);
        for(User u : users){
            System.out.println(u);
        }
    }

    @Test
    public void testFindByUser(){
        User u = new User();
        u.setUsername("王");
        u.setAddress("印度尼西峡");
        List<User> users = userDao.findByUser(u);
        for(User s : users){
            System.out.println(s);
        }
    }

    @Test
    public void testfindInIds(){
        QueryVo vo = new QueryVo();
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(6);
        list.add(5);
        vo.setIds(list);
        List<User> users = userDao.findInIds(vo);
        for(User s : users){
            System.out.println(s);
        }
    }
}
